//
//  NSString+YYExtension.m
//  NNHealthMall
//
//  Created by 蓓蕾 on 2021/7/17.
//  Copyright © 2021 YY. All rights reserved.
//

#import "NSString+YYExtension.h"

@implementation NSString (YYExtension)

#pragma mark - 检查字符串是否为空 YES:为空  NO:不为空
+ (BOOL)isBlankString:(NSString *)string
{
    if (![string isKindOfClass:[NSString class]]) {
        return YES;
    } else if (string == nil) {
        return YES;
    } else if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    } else if ([string isEqualToString:@"null"]) {
        return YES;
    } else if ([string isEqualToString:@"(null)"]) {
        return YES;
    } else if ([string isEqualToString:@"<null>"]) {
        return YES;
    } else if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    } else {
        return NO;
    }
}
#pragma mark - 检查字符串是否为空 YES:为空  NO:不为空
- (BOOL)isBlankString
{
    if (![self isKindOfClass:[NSString class]]) {
        return YES;
    } else if (self == nil) {
        return YES;
    } else if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    } else if ([self isEqualToString:@"null"]) {
        return YES;
    } else if ([self isEqualToString:@"(null)"]) {
        return YES;
    } else if ([self isEqualToString:@"<null>"]) {
        return YES;
    } else if ([self isEqualToString:@""]) {
        return YES;
    } else if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    } else {
        return NO;
    }
}

/// 获取文本宽度
- (CGFloat)yy_maxWidthWithFont:(UIFont*)font Height:(CGFloat)height Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    return [self yy_sizeWithFont:font Size:CGSizeMake(CGFLOAT_MAX, height) Alignment:alignment LinebreakMode:linebreakMode LineSpace:lineSpace].width;
}

- (CGFloat)yy_maxWidthWithFont:(UIFont*)font Height:(CGFloat)height
{
    return [self yy_sizeWithFont:font Size:CGSizeMake(CGFLOAT_MAX, height) Alignment:NSTextAlignmentLeft LinebreakMode:NSLineBreakByWordWrapping LineSpace:0].width;
}

- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width
{
    return [self yy_sizeWithFont:font Size:CGSizeMake(width, CGFLOAT_MAX) Alignment:NSTextAlignmentLeft LinebreakMode:NSLineBreakByWordWrapping LineSpace:0].height;
}

/// 获取文本高度
- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    return [self yy_sizeWithFont:font Size:CGSizeMake(width, CGFLOAT_MAX) Alignment:alignment LinebreakMode:linebreakMode LineSpace:lineSpace].height;
}
- (CGSize)yy_sizeWithFont:(UIFont*)font Size:(CGSize)size Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    if (self.length == 0) return CGSizeZero;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = linebreakMode;
    paragraphStyle.alignment = alignment;
    if (lineSpace > 0) paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    CGSize newSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:NULL].size;
    return CGSizeMake(ceil(newSize.width), ceil(newSize.height));
}

- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width ParagraphStyle:(NSParagraphStyle *)paragraphStyle
{
    if (![self isKindOfClass:[NSString class]] || self.length == 0) {
        return 0;
    }
    
    NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    CGSize newSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:NULL].size;
    return ceil(newSize.height);
}

/// 文字转图片
- (UIImage*)yy_textBecomeImageWithSize:(CGSize)size BackgroundColor:(UIColor*)color TextAttributes:(NSDictionary*)attributes{
    CGRect bounds = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, bounds);
    CGSize textSize = [self sizeWithAttributes:attributes];
    [self drawInRect:CGRectMake(bounds.size.width/2-textSize.width/2, bounds.size.height/2-textSize.height/2, textSize.width, textSize.height) withAttributes:attributes];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
/// 复制数据至剪切板
- (void)yy_pasteboard{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self;
}



@end
